﻿using AutoMapper;
using B.S.Packaged.Domain;
using B.S.Packaged.ErrorCode;
using B.S.Packaged.WebAPI.Write.Applications.Commands.MaterialPreparation;
using MediatR;
using SqlSugar;

namespace B.S.Packaged.WebAPI.Write.Applications.CommandHandlers
{
    public class UpdateMaterialPreparationHander : IRequestHandler<UpdateMaterialPreparationCommand, APIResult<int>>
    {

        private readonly IMapper mapper;
        private readonly ISqlSugarClient db;

        public UpdateMaterialPreparationHander(IMapper mapper, ISqlSugarClient db)
        {
            this.mapper = mapper;
            this.db = db;
        }

        /// <summary>
        /// 修改物料备料功鞥
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<APIResult<int>> Handle(UpdateMaterialPreparationCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();

            var model = mapper.Map<Domain.MaterialPreparation>(request);

            var yes = await db.Updateable<Domain.MaterialPreparation>(model).ExecuteCommandAsync();

            if (yes != null)
            {
                result.code = APIResultCode.success;
                result.msg = "修改物料备料信息成功";
            }
            else
            {
                result.code = APIResultCode.failed;
                result.msg = "修改物料备料信息失败";
            }

            return await Task.FromResult(result);
        }
    }
}
